//
//  OosCheckinDao.m
//  Oos4ios
//
//  Created by Pablo Gonzalez on 3/17/11.
//  Copyright 2011 11870.com. All rights reserved.
//

#import <oos4ios/DAOs/OosCheckinDao.h>
#import <oos4ios/Model/OosCheckin.h>
#import <oos4ios/Exceptions/MethodNotImplementedException.h>
#import "../../Atom/Queries/OosCheckinQuery.h"
#import "../../Atom/Feeds/OosCheckinFeed.h"
#import "../../Atom/Entries/OosCheckinEntry.h"
#import "../../Parsers/OosCheckinFeedParser.h"
#import "../../Parsers/OosCheckinEntryParser.h"
#import "OosDao+Internal.h"

@implementation OosCheckinDao

+ (OosCheckinDao *) daoFromUser:(OosUser *)user
{
	return [[[OosCheckinDao alloc] initFromUser: user] autorelease];
}

+ (OosCheckinDao *) daoFromUserSlug:(NSString *)userSlug
{
	return [[[OosCheckinDao alloc] initFromUserSlug: userSlug] autorelease];
}

- (OosCheckinDao *) initFromUserSlug:(NSString *)userSlug
{
	self = [self init];
	if (self) {
		self->mUserSlug = [userSlug retain];
	}
	return self;
}

- (OosCheckinDao *) initFromUser:(OosUser *)user
{
	self = [self initFromUserSlug: [user slug]];
	if (self) {
		self->mUser = [user retain];
	}
	return self;
}

#pragma mark Service Constructors 

+ (OosCheckinDao *) daoFromService:(OosService *)service
{
	return [[[OosCheckinDao alloc] initFromService: service] autorelease];
}

+ (OosCheckinDao *) daoFromServiceSlug:(NSString *)serviceSlug
{
	return [[[OosCheckinDao alloc] initFromServiceSlug: serviceSlug] autorelease];
}

- (OosCheckinDao *) initFromServiceSlug:(NSString *)serviceSlug
{
	self = [self init];
	if (self) {
		self->mServiceSlug = [serviceSlug retain];
	}
	return self;
}

- (OosCheckinDao *) initFromService:(OosService *)service
{
	self = [self initFromServiceSlug: [service slug]];
	if (self) {
		self->mService = [service retain];
	}
	return self;
}

- (OosDaoTicket *)createForService: (NSString *)serviceSlug 
                      withLatitude: (NSNumber *)latitude 
                     withLongitude: (NSNumber *)longitude
                       withComment: (NSString *)comment
                      withCallback: (id<IOosDaoCreateCallback>)callback
{
    OosCheckinOptions *checkinOptions = self.options.checkinOptions;
    checkinOptions.siteSlug = serviceSlug;
    checkinOptions.latitude = [latitude stringValue];
    checkinOptions.longitude = [longitude stringValue];
    if (comment) {
        checkinOptions.comment = comment;
    }
    return [super create:nil withCallback:callback];
}

- (OosDaoTicket *)create: (OosObject *)object 
            withCallback: (id<IOosDaoCreateCallback>)callback
{
    [MethodNotImplementedException raiseForSelector:_cmd inObject:self];
    return nil;
}

- (OosDaoTicket *)read:(id<IOosDaoReadCallback>)callback
{
    // TODO: the type should be set outside
//    if (!mUserSlug) {
//        self.options.checkinOptions.type = @"both";
//    } else {
        self.options.checkinOptions.type = @"grouped";
//    }
    return [super read:callback];
}

- (OosQuery *) query
{
	OosQuery *query = nil;
	if (mUserSlug) {
		query = [OosCheckinQuery queryFromUserSlug: mUserSlug];
	} else if (mServiceSlug) {
		query = [OosCheckinQuery queryFromServiceSlug: mServiceSlug];
	}
	if (query) {
		[query packStore: self.options];
		return query;
	} else {
		[NSException raise:@"Variables not set" 
					format: @"Need to provide information about a user or a service"];
		return nil;
	}
}

- (void)initAtomService:(OosAtomService *)atomService
{
    atomService.feedClass = [OosCheckinFeed class];
    atomService.entryClass = [OosCheckinEntry class];
}

- (id <IOosFeedParser>) feedParser
{
	return [OosCheckinFeedParser instance];
}

- (id<IOosEntryParser>)entryParser
{
    return [OosCheckinEntryParser instance];
}

- (void)dealloc
{
    [mUser release];
    [mUserSlug release];
    [mService release];
    [mServiceSlug release];
    [super dealloc];
}

@end
